/*
 * CS_UPGRADE_STONE_GACHYA.cpp
 *
 *  Created on: 2014. 3. 3.
 *      Author: mega
 */

#include "../../User.h"
#include "../../Command.h"
#include "../../NLogic/Item.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Function.h"
#include "../../NLogic/VirtualCurrency.h"
#include "../../NUtil/Utils.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

static void SendFailUpStoneGachya(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_UPGRADE_STONE_GACHYA);
	pUser->Send(pAck);
}

void Command::CS_UPGRADE_STONE_GACHYA(User * pUser, ::Network::Packet * pPacket)
{
/*	CS_UPGRADE_STONE_GACHYA	0x231A
	{
	}

	SC_UPGRADE_STONE_GACHYA	0x231B
	{
		s16		ack;

		s8		class_type;
		u8		sheet_type;
		u16		item_tid;

		u8		level;
		u8		item_quality;

		/// 강화석 효과는 이펙트 1번
		u8		eff_type1;
		u8		eff_pos1;
		u16		eff_value1;
	}*/

	PREPARE_PACKET;

	NLogic::Bag * pBag = pUser->GetBag(BAG_TYPE_ACCESSORY);
	if (NULL == pBag)
	{
		DEBUGLOG("%s : pBag is NULL!", __FUNCTION__);
		SendFailUpStoneGachya(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NResource::BalanceData * pBalance = NResource::Static::InstancePtr()->GetBalance()->GetBalancePtr(pUser);
	if (NULL == pBalance)
	{
		DEBUGLOG("%s : pBalance is NULL", __FUNCTION__);
		SendFailUpStoneGachya(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	int vcIndex = static_cast<int>(EnumVCIndex::EM_VCINDEX_UPSTONE_GACHYA);
	NResource::VCResource::Flyweight * pFly = NResource::Static::InstancePtr()->GetVCRef()->GetFly(vcIndex);
	if (NULL == pFly)
	{
		SendFailUpStoneGachya(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 보유젠 확인
	if (static_cast<int>(pUser->GetVirtualCurrency()->GetZen()) < (pFly->_VC_PRICE * -1))
	{
		SendFailUpStoneGachya(pUser, ACK_NOT_ENOUGH_ZEN);
		return;
	}

	NResource::UpStoneGachyaResource::UpStoneGachyaType eType = NResource::UpStoneGachyaResource::UpStoneGachyaType::EM_CHARGE_GACHYA;

	// 강화석 등급 결정
	int quality = 0;
	if (false == NResource::Static::InstancePtr()->GetUpStoneGachyaResource()->GetUpStoneGachyaQuality(eType, quality))
	{
		DEBUGLOG("%s : UpStone Quality Gachya Fail", __FUNCTION__);
		SendFailUpStoneGachya(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	int minLevel = pUser->GetCharacterInfo().level - pBalance->GetValue(EM_UPSTONE_GACHYA_LEVEL_VARIATION);
	if (minLevel <= 0)
	{
		minLevel = 1;
	}

	int maxLevel = pUser->GetCharacterInfo().level + pBalance->GetValue(EM_UPSTONE_GACHYA_LEVEL_VARIATION);
	int limitLevel = NResource::Static::InstancePtr()->GetCommon()->GetLimitLevel(pUser);
	if (maxLevel > limitLevel)
	{
		maxLevel = limitLevel;
	}

	// 강화석 데이터 생성
	NLogic::Item::Data clsData;
	clsData.quantity	= 1;
	clsData.class_type	= -1;
	clsData.tid			= UPSTONE_TID;
	clsData.sub_type	= CLASS_ITEM_TABLE_ETC;
	clsData.bag_type 	= BAG_TYPE_ACCESSORY;

	clsData.level		= NUtil::GetRandBetween(minLevel, maxLevel);
	clsData.quality		= quality;

	// 강화석 데이터 저장
	bool bBagFull = false;
	NLogic::Item * pItem = pBag->AddItemWithData(clsData, bBagFull);

	if (NULL == pItem)
	{
		if (true == bBagFull)
		{
	        NLogic::MailBox::Mail::Data clsMailData;

	        clsMailData.send_user_seq       = 0;
	        clsMailData.send_user_nickname  = "GAMEVIL";
	        clsMailData.recv_user_seq       = pUser->GetUserSeq();
	        clsMailData.gift_status         = EM_GIFT_ITEM;
	        clsMailData.item                = clsData;
	        clsMailData.message             = NResource::Static::InstancePtr()->GetMailMessageResource()->GetMessageItemGachya();

			// 아이템 부가 정보 세팅
			NResource::Static::InstancePtr()->GetItemResource()->SetAdditionalEffect(clsMailData.item);

	        if (false == NLogic::NFunction::SendMail(pUser, clsMailData))
	        {
	            DEBUGLOG("%s : false == NLogic::NFunction::SendMail(pUser, clsMailData)", __FUNCTION__);
	            return;
	        }

	    	// 응답
	    	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	    	*pAck << static_cast<int16_t>(ACK_INVEN_FULL_SEND_MAIL);
	    	*pAck << static_cast<int8_t>(clsMailData.item.class_type);
	    	*pAck << static_cast<int8_t>(clsMailData.item.sub_type);
	    	*pAck << static_cast<uint16_t>(clsMailData.item.tid);
	    	*pAck << static_cast<uint8_t>(clsMailData.item.level);
	    	*pAck << static_cast<uint8_t>(clsMailData.item.quality);
	    	*pAck << static_cast<uint8_t>(clsMailData.item.eff_type[1]);
	    	*pAck << static_cast<uint8_t>(clsMailData.item.eff_pos[1]);
	    	*pAck << static_cast<int16_t>(clsMailData.item.eff_value[1]);
	    	pAck->MakePacket(CMD_SC_UPGRADE_STONE_GACHYA);
	    	pUser->Send(pAck);

	    	// VC 처리
	    	NLogic::NFunction::VCProcess(pUser, vcIndex);

	        return;
		}
		else
		{
			DEBUGLOG("%s : pItem is NULL!!", __FUNCTION__);
			return;
		}
	}

	// 응답
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	*pAck << static_cast<int8_t>(pItem->GetData().class_type);
	*pAck << static_cast<int8_t>(pItem->GetData().sub_type);
	*pAck << static_cast<uint16_t>(pItem->GetData().tid);
	*pAck << static_cast<uint8_t>(pItem->GetData().level);
	*pAck << static_cast<uint8_t>(pItem->GetData().quality);
	*pAck << static_cast<uint8_t>(pItem->GetData().eff_type[1]);
	*pAck << static_cast<uint8_t>(pItem->GetData().eff_pos[1]);
	*pAck << static_cast<int16_t>(pItem->GetData().eff_value[1]);
	pAck->MakePacket(CMD_SC_UPGRADE_STONE_GACHYA);
	pUser->Send(pAck);

	// VC 처리
	NLogic::NFunction::VCProcess(pUser, vcIndex);
}


} /*NCommand*/ } /*ProjectZ*/
